package me.itsoo.artemis.framework.core.render;

/**
 * 代码行号渲染器
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/6/1
 */
public class LineNumberRender implements Render {

    private static final String FONT_STYLE = "font-size: inherit; font-family: inherit; line-height: inherit;";

    @Override
    public String doRend(String content) {
        final StringBuilder result = new StringBuilder()
                .append("<table style=\"")
                .append(FONT_STYLE)
                .append("border-collapse: collapse; border-spacing: 0;\">");
        final String[] splits = content.split("\n");

        for (int i = 0; i < splits.length; ++i) {
            result.append("<tr>");
            this.rendLineNumb(result, i + 1);
            this.rendCode(result, splits[i]);
            result.append("</tr>");
        }

        return result.append("</table>").toString();
    }

    private void rendLineNumb(StringBuilder sb, int number) {
        sb.append("<td style=\"")
                .append(FONT_STYLE)
                .append("-ms-user-select: none; -webkit-user-select: none; -moz-user-select: none;")
                .append("user-select: none; color: #babbbd; text-align: right; padding: 0 10px;\">")
                .append(number).append("</td>");
    }

    private void rendCode(StringBuilder sb, String code) {
        sb.append("<td style=\"")
                .append(FONT_STYLE)
                .append("word-wrap: normal; white-space: pre; overflow: visible;")
                .append("position: relative; vertical-align: top;\">")
                .append(code)
                .append("</td>");
    }
}
